let editor = {};
new Vue({
    el: "#pageContainer",
    data: {
        codeType: "html",
        sourceContent: "",
        resultContent: "",
        hasError: !1,
        compressInfo: ""
    },
    mounted: function() {
        (editor = CodeMirror.fromTextArea(this.$refs.codeSource, {
            mode: "htmlmixed",
            lineNumbers: !0,
            matchBrackets: !0,
            styleActiveLine: !0,
            lineWrapping: !0
        })).focus()
    },
    methods: {
        compress: function() {
            this.hasError = !1,
            this.compressInfo = "",
            this.sourceContent = editor.getValue().trim(),
            this.sourceContent ? "js" === this.codeType ? this.jsCompress(this.sourceContent) : "css" === this.codeType ? this.cssCompress(this.sourceContent) : this.htmlCompress(this.sourceContent) : alert("请先粘贴您需要压缩的代码")
        },
        changeCodeType(e) {
            editor.setOption("mode", {
                css: "text/css",
                js: {
                    name: "javascript",
                    json: !0
                },
                html: "htmlmixed"
            }[e]),
            editor.getValue().trim() && this.$nextTick(this.compress)
        },
        buildCompressInfo(e, t) {
            let s = e=>String(e).split("").reverse().join("").replace(/(...)(?!$)/g, "$1,").split("").reverse().join("")
              , o = e.length - t.length
              , r = e.length ? (100 * o / t.length).toFixed(2) : 0;
            this.compressInfo = "压缩前: <strong>" + s(e.length) + "</strong>，压缩后: <strong>" + s(t.length) + "</strong>，压缩率: <strong>" + s(o) + " (" + r + "%)</strong>"
        },
        jsCompress(e) {
            let t = UglifyJs3.compress(e);
            this.hasError = !!t.error,
            this.resultContent = t.out || t.error,
            !this.hasError && this.buildCompressInfo(this.sourceContent, this.resultContent)
        },
        cssCompress(e) {
            let t = e.replace(/\/\*(.|\n)*?\*\//g, "").replace(/\s*([\{\}\:\;\,])\s*/g, "$1").replace(/\,[\s\.\#\d]*\{/g, "{").replace(/;\s*;/g, ";").match(/^\s*(\S+(\s+\S+)*)\s*$/);
            this.resultContent = null === t ? e : t[1],
            this.buildCompressInfo(this.sourceContent, this.resultContent)
        },
        htmlCompress(e) {
            let t = {
                caseSensitive: !1,
                collapseBooleanAttributes: !0,
                collapseInlineTagWhitespace: !1,
                collapseWhitespace: !0,
                conservativeCollapse: !1,
                decodeEntities: !0,
                html5: !0,
                includeAutoGeneratedTags: !1,
                keepClosingSlash: !1,
                minifyCSS: !0,
                minifyJS: !0,
                preserveLineBreaks: !1,
                preventAttributesEscaping: !1,
                processConditionalComments: !0,
                processScripts: ["text/html"],
                removeAttributeQuotes: !0,
                removeComments: !0,
                removeEmptyAttributes: !0,
                removeEmptyElements: !1,
                removeOptionalTags: !0,
                removeRedundantAttributes: !0,
                removeScriptTypeAttributes: !0,
                removeStyleLinkTypeAttributes: !0,
                removeTagWhitespace: !0,
                sortAttributes: !0,
                sortClassName: !0,
                trimCustomFragments: !0,
                useShortDoctype: !0
            };
            t.log = console.log;
            try {
                this.resultContent = require("html-minifier").minify(e, t),
                this.buildCompressInfo(this.sourceContent, this.resultContent)
            } catch (e) {
                this.hasError = !0,
                this.resultContent = e
            }
        },
        toast(e) {
            window.clearTimeout(window.feHelperAlertMsgTid);
            let t = document.querySelector("#fehelper_alertmsg");
            if (t)
                t.innerHTML = e,
                t.style.display = "block";
            else {
                let s = document.createElement("div");
                s.innerHTML = '<div id="fehelper_alertmsg">' + e + "</div>",
                t = s.childNodes[0],
                document.body.appendChild(t)
            }
            window.feHelperAlertMsgTid = window.setTimeout(function() {
                t.style.display = "none"
            }, 3e3)
        },
        copyToClipboard(e) {
            if (this.hasError)
                return !1;
            let t = document.createElement("textarea");
            t.style.position = "fixed",
            t.style.opacity = 0,
            t.value = e,
            document.body.appendChild(t),
            t.select(),
            document.execCommand("Copy"),
            document.body.removeChild(t),
            this.toast("压缩结果已复制成功，随处粘贴可用！")
        }
    }
});
